//package medicalApp.velociraptors.com;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class PatientPatientView extends JDialog {

	/**
	 * The slider that adjusts the font size.
	 */ 
	private final JSlider slider;
	
	/**
	 * Determines the font size
	 */ 
	protected int fontSize;
	
	/**
	 * Displays the Patient's info.
	 */ 
	private JTextArea txtPatientInfo;
	
	/**
	 * Patient's SSN
	 */ 
	private final int SSN;
	
	/**
	 * The MedAppController that drives the file.
	 */ 
	@SuppressWarnings("unused")
	private MedAppController mac;
	
	/**
	 * The database connection.
	 */ 
	private Connection con;
	
	/**
	 * Date of Birth.
	 */ 
	@SuppressWarnings("unused")
	private String dob;
	
	/**
	 * The patient's username
	 */ 
	@SuppressWarnings("unused")
	private String username;
	
	/**
	 * The patient's firstName
	 */ 
	@SuppressWarnings("unused")
	private String firstName;
	
	/**
	 * The patient's lastName
	 */ 
	@SuppressWarnings("unused")
	private String lastName;
	
	/**
	 * Determines whether the patient's account is suspended
	 */ 
	@SuppressWarnings("unused")
	private int suspended;
	
	/**
	 * The patient's address
	 */ 
	@SuppressWarnings("unused")
	private String address;
	
	/**
	 * The patient's phone number
	 */ 
	@SuppressWarnings("unused")
	private long phoneNumber;
	
	/**
	 * The patient's email address
	 */ 
	@SuppressWarnings("unused")
	private String email;
	
	/**
	 * The patient's gender
	 */ 
	@SuppressWarnings("unused")
	private int gender;
	
	/**
	 * The patient's age
	 */ 
	@SuppressWarnings("unused")
	private int age;
	
	/**
	 * Represents the prescription
	 */ 
	@SuppressWarnings("unused")
	private final ArrayList<String> prescription;
	
	/**
	 * Represents blood test results
	 */ 
	@SuppressWarnings("unused")
	private final ArrayList<Integer> bloodTest;
	
	/**
	 * Represents urine test results
	 */ 
	@SuppressWarnings("unused")
	private final ArrayList<Integer> urineTest;
	
	/**
	 * Represents immunization records
	 */ 
	@SuppressWarnings("unused")
	private final ArrayList<Integer> immuno;
	
	/**
	 * Represents doctorOrders
	 */ 
	@SuppressWarnings("unused")
	private final ArrayList<String> instructions;
	
	/**
	 * Represents the doctor's first name
	 */ 
	private final ArrayList<String> t_doctorFirstName;
	
	/**
	 * Represents the doctor's last name
	 */ 
	private final ArrayList<String> t_doctorLastName;
	
	/**
	 * Represents the date and time of treatment
	 */ 
	private final ArrayList<String> t_dateAndTime;
	
	/**
	 * Represents the first name of the treating nurse
	 */ 
	private final ArrayList<String> t_nurseFirstName;
	
	/**
	 * Represents the last name of the treating nurse
	 */ 
	private final ArrayList<String> t_nurseLastName;
	
	/**
	 * Represents the symptoms
	 */ 
	private final ArrayList<String> t_symptoms;
	
	/**
	 * Systolic blood pressure
	 */ 
	private final ArrayList<Integer> t_bp_s;
	
	/**
	 * Diastolic blood pressure
	 */ 
	private final ArrayList<Integer> t_bp_d;
	
	/**
	 * Measured temperature
	 */ 
	private final ArrayList<Double> t_temp;
	
	/**
	 * Measured height
	 */ 
	private final ArrayList<Double> t_height;
	
	/**
	 * Measured weight
	 */ 
	private final ArrayList<Double> t_weight;
	
	/**
	 * Given diagnosis
	 */ 
	private final ArrayList<String> t_diagnosis;
	
	/**
	 * Output string
	 */ 
	private String output;
	

	/**
	 * Create the dialog.
	 */
	
	public PatientPatientView(final MedAppController mac, int currentSSN) {
		SSN = currentSSN; 
		this.mac = mac;
		dob = "";
		username = "";
		firstName = "";
		lastName = "";
		suspended = 0;
		address = "";
		phoneNumber = 0l;
		email = "";
		gender = 0;
		age = 0;
		prescription = new ArrayList<String>();
		bloodTest = new ArrayList<Integer>();
		urineTest = new ArrayList<Integer>();
		immuno = new ArrayList<Integer>();
		instructions = new ArrayList<String>();
		t_doctorFirstName = new ArrayList<String>();
		t_doctorLastName = new ArrayList<String>();
		t_dateAndTime = new ArrayList<String>();
		t_nurseFirstName = new ArrayList<String>();
		t_nurseLastName = new ArrayList<String>();
		t_symptoms = new ArrayList<String>();
		t_bp_s = new ArrayList<Integer>();
		t_bp_d = new ArrayList<Integer>();
		t_temp = new ArrayList<Double>();
		t_height = new ArrayList<Double>();
		t_weight = new ArrayList<Double>();
		t_diagnosis = new ArrayList<String>();
		con = mac.getCon();
		
		final String sql1 = "CREATE OR REPLACE VIEW  `patient_user_info` AS SELECT * FROM  `USER` WHERE  `USERTYPE` =  'Patient';";

		final String sql2 = "CREATE OR REPLACE VIEW  `patient_DOB_view` (DOB, SSN) AS SELECT  `DOB` , `P_SSN`FROM  `patients` WHERE 1;";

		final String sql3 = "CREATE OR REPLACE VIEW  `patient_dob_user_info_view` AS SELECT  `DOB` ,  `SSN` ,  `USERNAME` ,  `PASSWORD` ,  `U_FIRSTNAME` ,  `U_LASTNAME` ,  `SUSPENDED` FROM  `patient_DOB_view` JOIN  `patient_user_info` ON  `SSN` =  `patient_user_info`.`U_SSN`;";

		final String sql4 ="CREATE OR REPLACE VIEW `patient_user_dob_information_view` AS select `patient_dob_user_info_view`.`DOB` AS `DOB`,`patient_dob_user_info_view`.`SSN` AS `SSN`,`patient_dob_user_info_view`.`USERNAME` AS `USERNAME`,`patient_dob_user_info_view`.`PASSWORD` AS `PASSWORD`,`patient_dob_user_info_view`.`U_FIRSTNAME` AS `U_FIRSTNAME`,`patient_dob_user_info_view`.`U_LASTNAME` AS `U_LASTNAME`,`patient_dob_user_info_view`.`SUSPENDED` AS `SUSPENDED`,`patient_information`.`ADDRESS` AS `ADDRESS`,`patient_information`.`PHONE` AS `PHONE`,`patient_information`.`EMAIL` AS `EMAIL`,`patient_information`.`GENDER` AS `GENDER`,`patient_information`.`AGE` AS `AGE` from (`patient_information` join `patient_dob_user_info_view`  on((`patient_information`.`FOR_PATIENTSSN` = `patient_dob_user_info_view`.`SSN`)));"; 

		final String sql5=" CREATE OR REPLACE ALGORITHM = TEMPTABLE VIEW  `patient_user_dob_information_doctorsorders_view` AS SELECT  `SSN` ,  `DOB` ,  `USERNAME` ,  `PASSWORD` ,  `U_FIRSTNAME` ,  `U_LASTNAME` ,  `SUSPENDED` ,  `ADDRESS` ,  `PHONE` ,  `EMAIL` ,  `GENDER` , `AGE` ,  `PRESCRIPTION` ,  `BLOOD_TESTS` ,  `URINE_TESTS` ,  `IMMUNIZATIONS` ,  `INSTRUCTIONS` FROM  `doctorsorders` RIGHT JOIN  `patient_user_dob_information_view` ON  `DO_FORPATIENTSSN` =  `patient_user_dob_information_view`.`SSN`;";

		final String sql6 ="CREATE OR REPLACE ALGORITHM = TEMPTABLE VIEW  `patient_user_dob_information_doctorsorders_treatmentrecord_view` AS SELECT  `SSN` ,  `DOB` ,  `USERNAME` ,  `PASSWORD` ,  `U_FIRSTNAME` ,  `U_LASTNAME` ,  `SUSPENDED` ,  `ADDRESS` ,  `PHONE` , `EMAIL`,  `GENDER` ,  `AGE` ,  `PRESCRIPTION` ,  `BLOOD_TESTS` ,  `URINE_TESTS` ,  `IMMUNIZATIONS` ,  `INSTRUCTIONS` ,  `T_DOCTORFIRSTNAME` ,  `T_DOCTORLASTNAME` ,  `T_DATEANDTIME` , `T_NURSEFIRSTNAME` ,  `T_NURSELASTNAME` ,  `T_SYMPTOMS` ,  `T_BP_S`,`T_BP_D`,  `T_PULSE`,  `T_TEMPERATURE` ,  `T_HEIGHT` ,  `T_WEIGHT` ,  `T_DIAGNOSIS` FROM  `treatmentrecord` RIGHT JOIN  `patient_user_dob_information_doctorsorders_view` ON  `T_FORPATIENTSSN` =  `patient_user_dob_information_doctorsorders_view`.`SSN`;";

		String sql7 = "SELECT * FROM `patient_user_dob_information_doctorsorders_treatmentrecord_view` WHERE `SSN`='" + currentSSN + "'";
		try {
			Statement stmt1 = con.createStatement();
			Statement stmt2 = con.createStatement();
			Statement stmt3 = con.createStatement();
			Statement stmt4 = con.createStatement();
			Statement stmt5 = con.createStatement();
			Statement stmt6 = con.createStatement();
			Statement stmt7 = con.createStatement();
			
			int stmt = stmt1.executeUpdate(sql1);
			stmt = stmt2.executeUpdate(sql2);
			stmt = stmt3.executeUpdate(sql3);
			stmt = stmt4.executeUpdate(sql4);
			stmt = stmt5.executeUpdate(sql5);
			stmt = stmt6.executeUpdate(sql6);
			ResultSet rs = stmt7.executeQuery(sql7);
			
			int count = 0;
			
			while(rs.next()){
				dob = rs.getString("DOB");
				username = rs.getString("USERNAME");
				firstName = rs.getString("U_FIRSTNAME");
				lastName = rs.getString("U_LASTNAME");
				suspended = rs.getInt("SUSPENDED");
				address = rs.getString("ADDRESS");
				phoneNumber = rs.getLong("PHONE");
				email = rs.getString("EMAIL");
				gender = rs.getInt("GENDER");
				age = rs.getInt("AGE");
				prescription.add(rs.getString("PRESCRIPTION"));
				bloodTest.add(rs.getInt("BLOOD_TESTS"));
				urineTest.add(rs.getInt("URINE_TESTS"));
				immuno.add(rs.getInt("IMMUNIZATIONS"));
				instructions.add(rs.getString("INSTRUCTIONS"));
				t_doctorFirstName.add(rs.getString("T_DOCTORFIRSTNAME"));
				t_doctorLastName.add(rs.getString("T_DOCTORLASTNAME"));
				t_dateAndTime.add(rs.getString("T_DATEANDTIME"));
				t_nurseFirstName.add(rs.getString("T_NURSEFIRSTNAME"));
				t_nurseLastName.add(rs.getString("T_NURSELASTNAME"));
				t_symptoms.add(rs.getString("T_SYMPTOMS"));
				t_bp_s.add(rs.getInt("T_BP_S"));
				t_bp_d.add(rs.getInt("T_BP_D"));
				t_temp.add(rs.getDouble("T_TEMPERATURE"));
				t_height.add(rs.getDouble("T_HEIGHT"));
				t_weight.add(rs.getDouble("T_WEIGHT"));
				t_diagnosis.add(rs.getString("T_DIAGNOSIS"));
				count++;
			}
			String fullName ="Name: " + firstName + " " + lastName + "\n";
			mac.out.println(fullName);

			String userSuspended="";
			if(suspended == 0)
				 userSuspended = username + " is not suspended." + "\n";
			else
				userSuspended = username + " is suspended!" + "\n";
			
			mac.out.println(userSuspended);
			
			String contact = "Contact Information: \n" + address + "\n" + phoneNumber + "\n" + email + "\n";
			mac.out.println(contact);
			
			String bioData = "";
			if(gender == 0)
				bioData = "Biodata: \n Age: " + age + "\n Gender: Male" + "\n";
			else
				bioData = "Biodata: \n Age: " + age + "\n Gender: Female" + "\n";
			mac.out.println(bioData);
			
			String treatmentRecords = "";
			
			for(int i = 0;i < count;i++){
				treatmentRecords += "\n";

				treatmentRecords += "Doctor Name: " + t_doctorFirstName.get(i) + " " + t_doctorLastName.get(i) + "\n";
				treatmentRecords += "Attending Nurse: " + t_nurseFirstName.get(i) + " " + t_nurseLastName.get(i) + "\n";
				treatmentRecords += "Date: " + t_dateAndTime.get(i) + "\n";
				treatmentRecords += "Symptoms: " + t_symptoms.get(i) + "\n";
				treatmentRecords += "Blood Pressure: \n Systolic: " + t_bp_s.get(i) + " Diastolic: " + t_bp_d.get(i) + "\n";
				treatmentRecords += "Temperature: " + t_temp.get(i) + " F \n";
				treatmentRecords += "Height: " + t_height.get(i) + "\n Weight: " + t_weight.get(i) + "\n";
				treatmentRecords += "Diagnosis: " + t_diagnosis;

			}
			output = fullName + userSuspended + contact + bioData + treatmentRecords;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setResizable(false);
		setBounds(100, 100, 640, 480);
		getContentPane().setLayout(null);
		
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 604, 380);
		getContentPane().add(scrollPane);
		
		txtPatientInfo = new JTextArea(output);
		txtPatientInfo.setWrapStyleWord(true);
		txtPatientInfo.setLineWrap(true);
		scrollPane.setViewportView(txtPatientInfo);
		txtPatientInfo.setFont(new Font("Monospaced", Font.PLAIN, 14));
		txtPatientInfo.setEditable(false);
		
		slider = new JSlider();
		slider.setMinimum(10);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				JSlider source = (JSlider)arg0.getSource();
				
				if(!source.getValueIsAdjusting()){
					fontSize = (int)source.getValue();
					mac.out.println("Font Size set to: " + fontSize);
					txtPatientInfo.setFont(new Font("Monospaced", Font.PLAIN, fontSize));
				}
			}
		});
		slider.setValue(12);
		slider.setSnapToTicks(true);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.setMaximum(30);
		slider.setMajorTickSpacing(2);
		slider.setBounds(74, 402, 262, 35);
		getContentPane().add(slider);
		
		final JLabel lblTextSize = new JLabel("Text Size:");
		lblTextSize.setBounds(20, 412, 62, 14);
		getContentPane().add(lblTextSize);
	}
}
